<?php

namespace App\Http\Controllers\Api\Room;

use App\Http\Controllers\Api\Goods\GoodsLogic;
use App\Http\Controllers\Controller;
use App\Libs\Helper\CustomPaginate;
use App\Libs\Strategy\Context;
use App\Models\Goods\Cates;
use Flaravel\Upload\Fupload;
use Illuminate\Http\Request;

class GoodsCateController extends Controller
{
    private $cate;

    public function __construct()
    {
        $this->cate = new Context(new Cates());
    }

    public function index()
    {
        $cates = $this->cate->display(10,function($query){
            $query->where('pid',0);
            if(request()->keyword){
                $catesData = Cates::where('cate_name','like','%'.request()->keyword.'%')->get();
                $result = [];
                $catesData->filter(function($item,$key) use(&$result ){
                    if($item->pid != 0){
                        $res = Cates::where('id',$item->pid)->first();

                        if($res){
                            array_push($result, $res->id);
                        }
                    }
                    if($item->pid == 0){
                        array_push($result, $item->id);
                    }
                });
                $query->whereIn('id',$result);
            }
        });
        $cates->each(function($item){
            $cateRes = Cates::where('pid',$item->id)->get();
            if($cateRes){
                $item->children = $cateRes;
            }
        });
        return $cates;
    }

    public function create()
    {
        if(request()->id)
        {
            $cate = $this->cate->find(request()->id);
        }
        return view('goods_cate.add',compact('cate'));
    }

    public function store(Request $request)
    {
        $cates = Cates::where('cate_name',$request->cate_name1)->first();
        $data['pid'] = empty($cates->id)?0:$cates->id;
        $data['cate_name'] = $request->cate_name;
        $data['cate_img'] = $request->cate_img;
        if($this->cate->create($data))
        {
            return $this->success(['message'=>'添加成功']);
        };
        return $this->error('添加失败');
    }

    public function edit($id)
    {
        $cate = $this->cate->find($id);
        return view('goods_cate.edit',compact('cate'));
    }

    public function show($id)
    {
        if($id == 0)
        {
            return $this->success(GoodsLogic::getCate());
        }
    }

    public function update(Request $request,$id)
    {
        if($this->cate->update($id)){
            return $this->success(['message'=>'更新成功']);
        };
        return $this->error('更新失败');
    }

    public function destroy($id)
    {
        $cates = $this->cate->find($id);

        if($cates->pid == 0)
        {
            $catsData = Cates::where('pid',$cates->id)->get();
            if($catsData->isNotEmpty()){
                return $this->error('分类中含有子级不能删除！');
            }
            $this->cate->getModel()->where('id',$cates->id)->delete();
        }
        if($cates->cate_img)
        {
            Fupload::delete($cates->cate_img);
        }
        if($cates->delete())
        {
            return $this->success(['message'=>'删除成功']);
        }
        return $this->error('删除失败');
    }
}
